package com.sip.stream.utils.zfy.handler;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.util.Log;
import com.blankj.utilcode.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sip.stream.ClientApp;
import com.sip.stream.consumer.GB28181Consumer;
import com.sip.stream.gbstack.GS28181SDK;
import com.sip.stream.gbstack.GS28181SDKConfig;
import com.sip.stream.helper.GS28181Helper;
import com.sip.stream.utils.zfy.ZFYLocation;
import com.sip.stream.utils.zfy.ZFYPlatformCommonSettingViewModel;
import com.sip.stream.utils.zfy.ZFYUserInfo;
import com.sip.stream.utils.zfy.config.IPlatformMediaConfig;
import com.sip.stream.utils.zfy.config.ZFYMediaConfig;
import com.sip.stream.utils.zfy.consumer.IGSPlatformConsumer;
import com.sip.stream.utils.zfy.encoder.IGSVideoEncoder;
import com.sip.stream.utils.zfy.status.ZFYStatus;

import io.reactivex.Single;
import io.reactivex.functions.Function;
import java.util.Map;

public class GSCPlatformHandler implements IZFYPlatformHandler {
    private static final String TAG = "GSCPlatformHandler";

    private static final String SAVE_GATEWAY_URL = "SAVE_GATEWAY_URL";


    @Override
    public IGSPlatformConsumer createGroupintercomConsumer(IGSVideoEncoder iGSVideoEncoder) {
        return null;
    }

    @Override
    public Intent getGroupintercomIntent(Context context) {
        return null;
    }

    @Override
    public void inputAudioData(byte[] bArr) {
    }

    @Override
    public boolean needGroupintercomVideoEncoder() {
        return false;
    }

    @Override
    public void startBusy() {
    }

    @Override
    public void stopBusy() {
    }

    @Override
    public void switchAudioRecordMode(int model) {
    }

    @Override
    public void updateStatus(ZFYStatus zFYStatus) {
    }

    @Override
    public void setupApplication(Application application) {
        //初始化SDK
        GS28181Helper.getInstance().init(application.getApplicationContext());
    }

    @Override
    public void registerPlatform(Context context) {
        Log.i(TAG, "开始注册GB28181 服务");
        GS28181Helper.getInstance().init(context);
        if (!StringUtils.isEmpty(GS28181SDK.getInstance().getConfig().getServerIP()))
        {
            //version E8B_GXX_01
            GS28181Helper.getInstance().init(context).setDeviceBaseInfo(ZFYUserInfo.defaultUserInfo().getVirtualSNCode(), Build.MANUFACTURER, Build.MODEL, GS28181SDK.getInstance().getConfig().getFirmware(), "2016").setVideoChannelName(GS28181SDK.getInstance().getConfig().getVideoChannelName()).register();
        }
    }

    @Override
    public void unregisterPlatform() {
        GS28181Helper.getInstance().unregister(true);
    }

    @Override
    public void stopStreaming() {
        GS28181Helper.getInstance().stopStreaming();
    }

    @Override
    public void stopVideoStreaming() {
        GS28181Helper.getInstance().stopStreaming(0);
    }

    @Override
    public IGSPlatformConsumer createPlatformConsumer(IGSVideoEncoder iGSVideoEncoder) {
        if (iGSVideoEncoder == null) {
            return new GB28181Consumer();
        }
        return new GB28181Consumer(iGSVideoEncoder);
    }

    @Override
    public void syncPlatformMediaConfig(IPlatformMediaConfig iPlatformMediaConfig) {
        GS28181Helper.getInstance().syncPlatformMediaConfig(iPlatformMediaConfig);
    }

    @Override
    public void updateLocation(ZFYLocation zFYLocation) {
        GS28181Helper.getInstance().setLocation(zFYLocation);
    }

    @Override
    public void onKeyAction(int i) {
        if (i != 3) {
            return;
        }
        GS28181Helper.getInstance().postDeviceSOS();
    }

    @Override
    public Single<Boolean> setPlatformJsonSetting(String str) {
        Log.i(TAG, "setPlatformJsonSetting defaultURL " + str);
        GS28181Helper.getInstance().init(ClientApp.clientApp);
        return Single.error(new Throwable("没有可用配置"));
    }

    @Override
    public Single<Boolean> setPlatformJsonConfig(JsonObject jsonObject) {
        return Single.just(jsonObject).map(new Function<JsonObject, Boolean>() {
            @Override
            public Boolean apply(JsonObject jsonObject2) throws Exception {
                Gson gson = new Gson();
                Log.w(GSCPlatformHandler.TAG, "" + gson.toJson((JsonElement) jsonObject2));
                GS28181SDK.getInstance().init(ClientApp.clientApp);
                JsonObject asJsonObject = gson.toJsonTree(GS28181SDK.getInstance().getConfig()).getAsJsonObject();
                for (Map.Entry<String, JsonElement> entry : jsonObject2.entrySet()) {
                    asJsonObject.add(entry.getKey(), entry.getValue());
                }
                Log.w(GSCPlatformHandler.TAG, "" + gson.toJson((JsonElement) asJsonObject));
                GS28181SDKConfig gS28181SDKConfig = gson.fromJson((JsonElement) asJsonObject,GS28181SDKConfig.class);
                if (!StringUtils.isEmpty(gS28181SDKConfig.getDeviceGBCode())) {
                    if (StringUtils.isEmpty(gS28181SDKConfig.getAudioChannelID())) {
                        gS28181SDKConfig.setAudioChannelID(gS28181SDKConfig.getDeviceGBCode());
                    }
                    if (StringUtils.isEmpty(gS28181SDKConfig.getVideoChannelID())) {
                        gS28181SDKConfig.setVideoChannelID(gS28181SDKConfig.getDeviceGBCode());
                    }
                    if (StringUtils.isEmpty(gS28181SDKConfig.getSosChannelID())) {
                        gS28181SDKConfig.setSosChannelID(gS28181SDKConfig.getDeviceGBCode());
                    }
                }
                GS28181SDK.getInstance().setConfig(gS28181SDKConfig);
                GS28181SDK.getInstance().saveConfig();
                return Boolean.TRUE;
            }
        });
    }

    @Override
    public String getPlatformDeviceId() {
        return GS28181SDK.getInstance().getConfig().getDeviceGBCode();
    }

    @Override
    public boolean isRunning() {
        return GS28181Helper.getInstance().isRunning();
    }

    @Override
    public Single<Boolean> startBind(Context context, String str, String str2, String str3, String str4) {
        return Single.just(Boolean.TRUE);
    }

    @Override
    public void postAlarmTypeToPlatform(String str, String str2) {
        GS28181Helper.getInstance().postAlarmTypeToPlatform(str, str2);
    }


    @Override
    public void reportAction(String str, String str2) {
        Log.i(TAG, "reportAction " + str + " pParams" + str2);
        GS28181Helper.getInstance().reportAction(str, str2);
    }


    @Override
    public Single<Boolean> startFaceBind(String str) {
        return Single.just(Boolean.FALSE);
    }
}
